package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.model.entity.ConfThirdPart;
import com.jic.point.model.entity.ConfThirdPartApply;
import com.jic.point.model.vo.request.ConfThirdPartApplyRequest;
import com.jic.point.model.vo.response.ConfThirdPartApplyResponse;
import com.jic.point.service.ConfThirdPartApplyService;
import com.jic.point.service.ConfThirdPartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author gym
 * @date 2020-10-22
 */


@RestController
@Slf4j
@RequestMapping("/confThirdPartApply")
@Api(tags = " [第三方机构副表]控制类-PC")
public class ConfThirdPartApplyController {

    @Autowired
    private ConfThirdPartApplyService service;

    @Autowired
    private ConfThirdPartService ctpaservice;


    /**
     * 新增记录
     *
     * @param request 新增[第三方机构主表]请求参数
     * @return ConfThirdPartResponse 新增[第三方机构主表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[第三方机构主表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        ConfThirdPartApply confThirdPartApply = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, confThirdPartApply);
        long flag = service.save(confThirdPartApply);
        return RestResult.success(flag);
    }

    /**
     * 新增记录
     *
     * @param request 新增[第三方机构主表]请求参数
     * @return ConfThirdPartResponse 新增[第三方机构主表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[第三方机构主表]记录")
    @PostMapping("/insertSelective")
    @ResponseBody
    public RestResult<Long> insertSelective(@RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        ConfThirdPartApply confThirdPartApply = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, confThirdPartApply);
        long flag = service.insertSelective(confThirdPartApply);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[第三方机构主表]请求参数
     * @return ConfThirdPartResponse 新增或修改[第三方机构主表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[第三方机构副表]")
    @PostMapping("/saveOrUpdateApply")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfThirdPartApplyRequest request) throws Throwable {

        ConfThirdPartApply confThirdPartApply = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, confThirdPartApply);
        long flag = service.saveOrUpdate(confThirdPartApply);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     * @param request 删除[第三方机构主表]请求参数
     * @return ConfThirdPartResponse 删除[第三方机构主表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[第三方机构主表]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        ConfThirdPartApply confThirdPartApply = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, confThirdPartApply);
        long flag = service.deleteByPrimaryKeyLogically(confThirdPartApply);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     * @param request 修改[第三方机构主表]请求参数
     * @return ConfThirdPartResponse 修改[第三方机构主表]响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[第三方机构主表]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        ConfThirdPartApply confThirdPartApply = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, confThirdPartApply);
        long flag = service.updateByPrimaryKeySelective(confThirdPartApply);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     * @param request 查询[第三方机构主表]详情请求参数
     * @return ConfThirdPartResponse 查询[第三方机构主表]详情响应参数
     * author tzc
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[第三方机构主表]详情")
    @ResponseBody
    public RestResult<ConfThirdPartApplyResponse> getByPrimaryKey(@Validated @RequestBody ConfThirdPartApplyRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            ConfThirdPartApply confThirdPart = new ConfThirdPartApply();
            BeanUtils.copyProperties(request, confThirdPart);
            ConfThirdPartApply result = service.getByPrimaryKey(confThirdPart);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            ConfThirdPartApplyResponse bo = new ConfThirdPartApplyResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new ConfThirdPartApplyResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<ConfThirdPartApply>> listPage(@RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        ConfThirdPartApply param = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }


    /**
     * CONF_THIRD_PART 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [第三方机构主表] ")
    @ResponseBody
    public RestResult<List<ConfThirdPartApplyResponse>> listConfThirdPartAllRecord(@RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        ConfThirdPartApply param = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<ConfThirdPartApply> list = service.listAllRecord(param);
        List<ConfThirdPartApplyResponse> responseList = list.stream().map(temp -> {
            ConfThirdPartApplyResponse bo = new ConfThirdPartApplyResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 审核提交
     * @param request
     * @return
     * @throws Throwable
     */
    @PostMapping("/auditAubmit")
    @ApiOperation(value = "审核提交 [第三方机构主表] ")
    @ResponseBody
    public RestResult<Long> auditAubmit(@Validated @RequestBody ConfThirdPartApplyRequest request) throws Throwable {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfThirdPartApply confThirdPartApply = new ConfThirdPartApply();
        BeanUtils.copyProperties(request, confThirdPartApply);
        ConfThirdPartApply result = service.getByPrimaryKey(confThirdPartApply);
        //赋值
        result.setAuditSts(request.getAuditSts());
        result.setAuditReason(request.getAuditReason());
        //审核成功
        if (result.getAuditSts() == 1) {
            //审核成功------新增
            if (result.getCurrentAction() == 0) {
                //申请表逻辑删除
                result.setAuditorId(request.getAdminOperate().getOperator());
                request.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //插入到主表中
                ConfThirdPart confThirdPart = new ConfThirdPart();
                BeanUtils.copyProperties(result, confThirdPart);
                confThirdPart.setId(null);
                confThirdPart.setDeleteFlag(0);
                long flag = ctpaservice.insertSelective(confThirdPart);
                return RestResult.success(flag);
            } else if (result.getCurrentAction() == 1) {
                //审核成功-------编辑-------申请表逻辑删除
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                //主表，通过修改code--编辑逻辑删除为0
                ConfThirdPart confThirdPart = new ConfThirdPart();
                BeanUtils.copyProperties(result, confThirdPart);
                confThirdPart.setDeleteFlag(0);
                //confThirdPart.setAuditorId(result.getAdminOperate().getOperator());
                long flag = ctpaservice.saveOrUpdateApply(confThirdPart);
                return RestResult.success(flag);
            } else {
                //审核成功--------删除---申请表删除
                result.setAuditorId(request.getAdminOperate().getOperator());
                result.setDeleteFlag(1);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            }
        } else if(result.getAuditSts() == 2){
            //审核失败------新增失败
            if (result.getCurrentAction() == 0) {
                //申请表逻辑删除
                // result.setAuditorId(request.getAdminOperate().getOperator());
                result.setDeleteFlag(1);
                long flag = service.deleteByPrimaryKeyLogically(result);
                return RestResult.success(flag);
            } else if (result.getCurrentAction() == 1 || result.getCurrentAction() == 2) {
                //删除、编辑失败，申请表逻辑删除，主表逻辑删除改为1
                result.setDeleteFlag(1);
                service.deleteByPrimaryKeyLogically(result);
                ConfThirdPart confThirdPart = new ConfThirdPart();
                BeanUtils.copyProperties(result, confThirdPart);
                confThirdPart.setDeleteFlag(0);
                long flag = ctpaservice.saveOrUpdateApply(confThirdPart);
                return RestResult.success(flag);
            }

        }else{
            result.setAuditSts(3);
            long flag = service.deleteByPrimaryKeyLogically(result);
            return RestResult.success(flag);
        }
        return null;
    }
}
